/**
 * 
 */
package com.umakr.ax.core.base;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.umakr.ax.utils.WebUtil;
import org.springframework.ui.ModelMap;

import com.baomidou.mybatisplus.plugins.Page;

/**
 * 控制器基类
 *
 * @author gx
 */
public abstract class AbstractController extends BaseController {
    protected BaseService service;


    public Object query(ModelMap modelMap, Map<String, Object> param) {
        Page<?> list = getService().query(param);
        return setSuccessModelMap(modelMap, list);
    }

    public Object queryList(ModelMap modelMap, Map<String, Object> param) {
        List<?> list = getService().queryList(param);
        return setSuccessModelMap(modelMap, list);
    }

    public Object get(ModelMap modelMap, BaseModel param) {
        BaseModel result = getService().queryById(param.getId());
        return setSuccessModelMap(modelMap, result);
    }

    @SuppressWarnings("unchecked")
    public Object update(ModelMap modelMap, BaseModel param) {
        getService().update(param);
        return setSuccessModelMap(modelMap);
    }

    public Object delete(ModelMap modelMap, BaseModel param) {
        getService().delete(param.getId());
        return setSuccessModelMap(modelMap);
    }


    @SuppressWarnings("unchecked")
    @Override
    public <T extends BaseService> T getService() {
        if(this.service == null){
            String serviceName = getClass().getSimpleName().replace("Controller","Service");
            this.service =(BaseService)WebUtil.getBean( StringUtils.firstToLowerCase(serviceName));
        }
        return (T)this.service;
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T extends BaseService> T getService(Class<T> clazz) {
        if(this.service == null){
            this.service = WebUtil.getBean(clazz);
        }
        return (T)this.service;
    }

}
